﻿using MediatR;
using ProductManagement.Applcation.Command;
using ProductManagement.Infrstructre.Interface;

namespace ProductManagement.Applcation.CommandHanlers
{
    /// <summary>
    /// 产品删除中介者
    /// </summary>
    public class ProductDeleteCommandHandler:IRequestHandler<ProductDeleteCommand,int>
    {
        private readonly InterfaceProductRepository _productRepository;

        public ProductDeleteCommandHandler(InterfaceProductRepository productRepository)
        {
            _productRepository = productRepository;
        }

        public async Task<int> Handle(ProductDeleteCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            foreach (var item in request.id)
            {
                var res = (await _productRepository.GetAll()).First(x => x.ProductId == item);
                if (res!=null)
                {
                    res.IsDel = true;
                    res.DelName = request.DelName;
                    res.DelTime = DateTime.Now;
                    await _productRepository.UpdateAsync(res);
                    count = 1;
                }
                else
                {
                    count = -1;//不存在
                }
            }
            return count;
        }





    }
}
